package com.zlj.jdbc.jdbcUtils02;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.mysql.jdbc.Statement;

/**
 * JdbcUtils的第二次修改版本
 * @author bashen
 *
 */
public class JdbcUtils {
	//使用配置文件的默认配置，所以要求必须给出c3p0-config.xml,且配置文件的命名必须为c3p0-config.xml，放在src下
    private static ComboPooledDataSource dataSource;
    
    static {
    	dataSource = new ComboPooledDataSource();
    }
    
    /**
     * 使用连接池返回一个连接对象
     * @return
     * @throws SQLException
     */
    public static Connection getConnection() throws SQLException {
    	return dataSource.getConnection();
    }
    
    /**
     * 返回连接池对象
     * @return
     */
    public static DataSource getDataSource() {
    	return dataSource;
    }
    
    /**
     * 释放资源
     * @param conn
     * @param statement
     * @param resultSet
     */
    public static void release(Connection conn, Statement statement, ResultSet resultSet) {
    		try {
    			if(resultSet != null) {
    				resultSet.close();
    			}
    			
    			if(statement != null) {
    				statement.close();
    			}
    			
    			if(conn != null) {
    				conn.close();
    			}
			} catch (SQLException e) {
				e.printStackTrace();
			}
    	}
}
